package edu.hcmus.sow.dao;

import java.util.List;

import org.hibernate.criterion.Restrictions;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import edu.hcmus.sow.domain.File;

/**
 * DAO to manage File entities.
 * 
 */
@Repository
@Transactional
public class FileDAOImpl extends AbstractHibernateDAO<File, Integer> implements FileDAO {

   public FileDAOImpl() {
      super();
   }

   public File findByName(String fileName) throws DataAccessException {
      List<File> list = findByCriteria(Restrictions.eq("name", fileName));
      return list.size() == 0 ? null : list.get(0);
   }

   public List<File> getByFolderID(Integer folderID) throws DataAccessException {
      List<File> list = findByCriteria(Restrictions.eq("folderID", folderID));
      return list;
   }

   public boolean isExist(Integer folderID, String name) throws DataAccessException {
      List<File> list = findByCriteria(Restrictions.eq("folderID", folderID), Restrictions.eq("name", name));

      if (list.isEmpty()) {
         return false;
      }
      return true;
   }

}
